<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="keyword" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="keywordQuery">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表	-->
    <!--列表
   @selection-change="selsChange"
   -->
    <el-table :data="shop" highlight-current-row v-loading="listLoading"
              @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" label="序号" width="60">
      </el-table-column>
      <el-table-column prop="name" label="店铺名称" width="100" sortable>
      </el-table-column>
      <el-table-column prop="tel" label="店铺电话" width="100" sortable>
      </el-table-column>
      <el-table-column prop="registerTime" label="成立时间" width="100" sortable>
      </el-table-column>
      <el-table-column prop="address" label="店铺地址" width="100" sortable>
      </el-table-column>
      <el-table-column prop="logo" label="店铺商标" width="100" sortable>
      </el-table-column>
      <el-table-column prop="emp.username" label="店长编号" width="100" sortable>
      </el-table-column>
      <el-table-column prop="state" label="店铺状态" width="100" sortable>
        <template slot-scope="scope">
          <span style="color: green" v-if="scope.row.state==4">注销</span>
          <span style="color: green" v-if="scope.row.state==3">营业</span>
          <span style="color: green" v-if="scope.row.state==2">试营业</span>
          <span style="color: green" v-if="scope.row.state==1">审核</span>
          <span style="color: red" v-else-if="scope.row.state==0">停业</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.index,scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="totals" style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="部门编号" prop="sn">
          <el-input v-model="editForm.sn" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="部门状态">
          <el-radio-group v-model="editForm.state">
            <el-radio class="radio" :label="1">启用</el-radio>
            <el-radio class="radio" :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="上级部门">
          <!--
          el-cascader - 级联
            :options="deptTree" - 级联框中需要的数据【集合】
          回显机制：
            v-model="saveForm.parent"
            parent：[2,5]
        -->
          <el-cascader v-model="editForm.parent_id"
                       :options="deptTree"
                       :props="{
            	checkStrictly: true,
                label: 'name', // 级联框显示那个属性的值
                value: 'id'    // 回显时需要,回传值的时候也需要
            }" clearable>
          </el-cascader>
        </el-form-item>

        <el-form-item label="部门经理" prop="manager_id">
          <!--          <el-input v-model="editForm.manager_id" auto-complete="off"></el-input>-->

          <el-select  clearable v-model="editForm.manager_id" filterable placeholder="请选择">
            <el-option
                v-for="item in employees"
                :label="item.username"
                :value="item.id">
              <span style="float: left">{{ item.username }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.phone }}</span>
            </el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

    <!--新增界面-->
    <!--		<el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
          <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="addForm.name" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="性别">
              <el-radio-group v-model="addForm.sex">
                <el-radio class="radio" :label="1">男</el-radio>
                <el-radio class="radio" :label="0">女</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="年龄">
              <el-input-number v-model="addForm.age" :min="0" :max="200"></el-input-number>
            </el-form-item>
            <el-form-item label="生日">
              <el-date-picker type="date" placeholder="选择日期" v-model="addForm.birth"></el-date-picker>
            </el-form-item>
            <el-form-item label="地址">
              <el-input type="textarea" v-model="addForm.addr"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click.native="addFormVisible = false">取消</el-button>
            <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
          </div>
        </el-dialog>-->
  </section>
</template>

<script>


export default {
  data() {
    return {
      employees:[],
      title: "",
      filters: {
        name: ''
      },
      deptTree: [],
      Departments: [],
      totals: 0,
      pageSize: 8,
      currentPage:1,
      listLoading: false,
      sels: [],//列表选中列
      keyword:null,

      editFormVisible: false,//编辑界面是否显示
      editLoading: false,


      editFormRules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' }
        ],
        sn: [
          { required: true, message: '请输入部门编号', trigger: 'blur' }
        ]
      },
      //编辑界面数据
      editForm: {
        id: null,
        sn: '',
        name: '',
        state: null,
        manager_id: null,
        parent_id: null
      },
    }
  },
  methods: {
    getDeptTree(){
      this.$http.get("/department/deptTree").then(res=>{
        this.deptTree = res.data;
      })
    },


    getemployee(){
      this.$http.get("/emp").then(res=>{
        this.employees=res.data
      })
    },
    keywordQuery(){
      this.currentPage=1;
      this.getDepartments();

    },

    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      this.getDepartments();
    },
    //获取用户列表
    getDepartments() {
      let para = {
        currentPage: this.currentPage,//当前页
        pageSize:this.pageSize,//每页显示的条数
        //name: this.filters.name
        //高级查询参数 - 关键字
        keyword: this.keyword
      };
      this.listLoading = true;//显示加载框/忙等框
      this.$http.post("/shop",para).then(res=>{
        //给totals赋值 - 计算分页栏
        this.Departments=res.data.data;
        //赋值分页数据 - 显示当前页的分页数据
        this.totals=res.data.totals;
        //隐藏加载框/忙等框
        this.listLoading=false;
      }).catch(res=>{
        this.$message.error("下班了，小爷不伺候了，明天再来吧。。。。")
      })
      //NProgress.start();
      getDepartmentListPage(para).then((res) => {
        this.total = res.data.total;
        this.Departments = res.data.Departments;
        this.listLoading = false;
        //NProgress.done();
      });
    },


    //删除
    handleDel: function (row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;

        this.$http.delete("/department/"+row.id).
        then(res=>{
          this.listLoading = false;
          if(res.data.success){
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          }else{
            this.$message({
              message:res.data.msg,
              type: 'error'
            });
          }
          this.currentPage=1;
          this.getDepartments();
        }).catch(res=>{
          this.$message({
            message: '系统加载中',
            type: 'error'
          });
        })
        //NProgress.start();
        let para = { id: row.id };
        removeDepartment(para).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getDepartments();
        });
      }).catch(() => {

      });
    },



    //显示编辑界面
    handleEdit: function (index, row) {
      this.title = "编辑";
      this.getemployee();
      this.editFormVisible = true;
      this.getDeptTree();
      // 可控一份数据，然后回显数据
      this.editForm = Object.assign({}, row);
    },


    //显示新增界面
    handleAdd: function () {
      this.title = "新增";
      this.getemployee();
      this.getDeptTree();
      this.editFormVisible = true; // 弹出模态框
      // 清空模态框
      this.editForm = {
        id: null,
        sn: '',
        name: '',
        state: null,
        manager_id: null,
        parent_id: null,
      };
    },


    //编辑
    // 提交按钮
    editSubmit: function () {
      // 表单校验
      this.$refs.editForm.validate((valid) => {
        if (valid) {

          this.$confirm('确认提交吗？', '温馨提示', {}).
          then(() => {
            this.editLoading = true;  // 忙等框

            // 克隆数据
            let para = Object.assign({}, this.editForm);
            let arr = para.parent_id
            if(arr){
              para.parent_id = null;
            }
            if(arr && arr.length>0){
              para.parent_id = arr[arr.length-1]
            }

            this.$http.put("/shop",para)
                .then(res=>{
                  this.editLoading = false; // 关闭忙等框
                  if(res.data.success){
                    this.$message({
                      message: '操作成功',
                      type: 'success'
                    });
                  }else{
                    this.$message({
                      message: res.data.msg,
                      type: 'error'
                    });
                  }
                  // 关闭模态框
                  this.editFormVisible = false;
                  // 刷新页面
                  this.getDepartments();

                }).catch(res=>{
              this.$message({
                message: '系统异常，稍后重试',
                type: 'error'
              });
            })

          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {

      var ids = this.sels.map(item => item.id);  // [1,2,3,4,5,6]

      this.$confirm('确认删除选中记录吗？', '温馨提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true; // 开启忙等框

        this.$http.patch("/shop",ids)
            .then(res=>{
              // 关闭忙等框
              this.listLoading = false;
              if(res.data.success){
                this.$message({
                  message: '删除成功',
                  type: 'success'
                });
              }else{
                this.$message({
                  message: res.data.msg,
                  type: 'error'
                });
              }
              this.currentPage = 1;
              this.getDepartments();
            }).catch(res=>{
          this.$message({
            message: '系统异常，稍后重试',
            type: 'error'
          });
        })





        //NProgress.start();
        /*	let para = { ids: ids };
          batchRemoveDepartment(para).then((res) => {
            this.listLoading = false;
            //NProgress.done();
            this.$message({
              message: '删除成功',
              type: 'success'
            });
            this.getDepartments();
          });*/
      }).catch(() => {

      });
    }
  },


  mounted() {
    this.getDepartments();
  }
}

</script>

<style scoped>

</style>